//
//  M_LogisticsVC.swift
//  letuofu
//
//  Created by WH-JS on 2020/1/7.
//  Copyright © 2020 WH-JS. All rights reserved.
//

import UIKit
import MBProgressHUD

class M_LogisticsVC: BaseViewControllerWithHeader {
    let tableView = QuickTableView<[String:Any?],LogisticsCell>()
    let titleLab = UILabel()
    let subtitleLab = UILabel()
    var orderID : String
    
    lazy var defultView : UIView = {
        let v = UIView()
        v.backgroundColor = .xF2F2F2
        let img = v.drawImage(named: "zhaobudaoyemian_114") { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview()
            make.height.width.equalTo(200)
        }
        img.contentMode = .scaleAspectFit
        
        v.drawLabel(text: "暂无物流信息", fontSize: 19, textColor: .x333333) { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(img.snp.bottom).offset(10)
        }.textAlignment = .center
        return v
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        setupUI()
        getLogisticsData(orderID: orderID, callback: { (data) in
            if data.count == 0 {
                self.defultView.isHidden = false
            }
            else {
                self.defultView.isHidden = true
            }
            self.tableView.list = data
            self.tableView.reloadData()
        })
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        LogisticsCell.cellHeightCache.removeAll()
    }
    
    init(orderid : String) {
        self.orderID = orderid
        super.init()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */
    
    func getLogisticsData(orderID: String, callback: @escaping ([[String:Any?]]) -> Void) {
        MBProgressHUD.showOrIncrease()
        let params :[String:Any] = ["identityToken":LocalStorage.getString(forKey: USER_IDENTITYTOKEN) ?? "",
                                    "orderID":orderID]
        API.request(url: .M_GetLogisticsInfo, params: params) { (result) in
            MBProgressHUD.hideOrDecrease()
            if result.success {
                if let data = result.data?.getDictionary("data"),let dl = data.getDictList("items") {
                    self.titleLab.text = "物流公司:" + (data.getString("logisticsCompany") ?? "")
                    self.subtitleLab.text = "物流单号：" + (data.getString("logisticsOrderNo") ?? "")
                    callback(dl)
                }
                else {
                    self.defultView.isHidden = false
                }
            }
            else {
                self.defultView.isHidden = false
                let popup = PopupSureBox(title:"获取订单失败", sureText: "确定")
                popup.show(vc: UIViewController.topMostController() ?? self)
                popup.onSureButtonClick = {
                    //确定执行
                }
            }
        }
    }
}


// MARK: UI
extension M_LogisticsVC {
    func setupUI() {
        safeView.backgroundColor = .xF2F2F2
        headView.backgroundColor = .white
        headTitle.text = "物流详情"
//        drawRightButton(imageName: "xiaoxi_4")
//        .addTarget(self, action: #selector(_xiaoxiButtonClick(button:)), for: .touchUpInside)
        drawRightButton(imageName: "xiangqingkefu_82")
        .addTarget(self, action: #selector(_kefuButtonClick(button:)), for: .touchUpInside)
        
        let bgview = safeView.drawView { (make) in
            make.top.equalTo(headView.snp.bottom).offset(10)
            make.left.right.equalToSuperview()
            make.height.equalTo(72)
        }
        bgview.backgroundColor = .white
        
        bgview.drawLabel(label: titleLab, text: "物流公司:", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.top.equalToSuperview().offset(20)
            make.left.equalToSuperview().offset(10)
        }
        bgview.drawLabel(label: subtitleLab, text: "物流单号：", fontSize: 12, fontWeight: .regular, textColor: .x333333) { (make) in
            make.top.equalToSuperview().offset(40)
            make.left.equalToSuperview().offset(10)
        }
        
        safeView.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(bgview.snp.bottom).offset(10)
        }
        //刷新相关事件绑定
        tableView.onSetCellSubject().bindWeak(self) { (cell, s) in
            
        }
        
        //当点击表格行时
        tableView.onDataSelectedSubject().bindWeak(self) { (data, s) in
        }

        safeView.drawView(view: defultView, { (make) in
            make.top.equalTo(headView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        })
    }
}

// MARK: event & action
extension M_LogisticsVC {
    @objc func _kefuButtonClick(button: Button) {
        let vc = M_SimpleArticleVC()
        pushViewController(vc)
        let qidiandic = LocalStorage.getDic(forKey: QIDIAN_KEY)
        vc.loadUrl(qidiandic?.getString("id") ?? "https://url.cn/50hfdES?_type=wpa&qidian=true")

        vc.headTitle.text = "联系客服"
    }
    
    
    @objc func _xiaoxiButtonClick(button: Button) {
        let vc = M_NewsListVC()
        pushViewController(vc)
    }
}
